Resource = class('Resource', Entity):include(Stateful)

function Resource:initialize(x, y, mode, count)
	Entity.initialize(self)
	self.x = x or 0
	self.y = y or 0	
	self.resource = mode
	self.res_count = count
	self.draw_points = {}
	self:set_base_draw_points()
	self:gotoState(mode)
end

function Resource:adapt_surrounding_tiles()
	self.surrounding_tiles ={}
	--print("finding surrounding tiles")
	
	self.surrounding_tiles.ul = game:get_tile(self.x-game.tile_size, self.y-game.tile_size)
	self.surrounding_tiles.u = game:get_tile(self.x, self.y-game.tile_size)
	self.surrounding_tiles.ur = game:get_tile(self.x+game.tile_size, self.y-game.tile_size)
	self.surrounding_tiles.l = game:get_tile(self.x-game.tile_size, self.y)
	self.surrounding_tiles.r = game:get_tile(self.x+game.tile_size, self.y)
	self.surrounding_tiles.dl = game:get_tile(self.x-game.tile_size, self.y+game.tile_size)
	self.surrounding_tiles.d = game:get_tile(self.x, self.y+game.tile_size)
	self.surrounding_tiles.dr = game:get_tile(self.x+game.tile_size, self.y+game.tile_size)

	local min_x, min_y, max_x, max_y, num

	if self.surrounding_tiles.ul then		
		self:populate_points(1, 1, 6, 6, self:resource_to_num(self.surrounding_tiles.ul.resource))
	end

	if self.surrounding_tiles.u then		
		self:populate_points(7, 1, 10, 6, self:resource_to_num(self.surrounding_tiles.u.resource))
	end

	if self.surrounding_tiles.ur then		
		self:populate_points(11, 1, 16, 6, self:resource_to_num(self.surrounding_tiles.ur.resource))
	end

	if self.surrounding_tiles.l then		
		self:populate_points(1, 7, 6, 10, self:resource_to_num(self.surrounding_tiles.l.resource))
	end

	if self.surrounding_tiles.r then		
		self:populate_points(11, 7, 16, 10, self:resource_to_num(self.surrounding_tiles.r.resource))
	end

	if self.surrounding_tiles.dl then		
		self:populate_points(1, 11, 6, 16, self:resource_to_num(self.surrounding_tiles.dl.resource))
	end

	if self.surrounding_tiles.d then		
		self:populate_points(7, 11, 10, 16, self:resource_to_num(self.surrounding_tiles.d.resource))
	end

	if self.surrounding_tiles.dr then		
		self:populate_points(11, 11, 16, 16, self:resource_to_num(self.surrounding_tiles.dr.resource))
	end

	self:populate_points(7, 7, 10, 10, self:resource_to_num(self.resource))
	

end

function Resource:populate_points(min_x, min_y, max_x, max_y, num)
	for y = min_y, max_y do
		for x = min_x, max_x do
			self.draw_points[y][x] = num
		end
	end

end

function Resource:resource_to_num(res)
	if res == "water" then
		return 0
	elseif res == "soil" then
		return 1
	elseif res == "plant" then
		return 2
	end	
end

function Resource:num_to_color(num)
	if num == 0 then
		return game.color_water
	elseif num == 1 then
		return game.color_soil
	elseif num == 2 then
		return game.color_plant
	end
end


function Resource:set_base_draw_points()
	local num = self:resource_to_num(self.resource)

	for y = 1, game.tile_size do
		self.draw_points[y] = {}
		for x = 1, game.tile_size do
			self.draw_points[y][x] = num
		end
	end
end

function Resource:draw()

	if self.draw_points then
		for y = 1, game.tile_size do
			for x = 1, game.tile_size do
				--print(self.draw_points[y][x])
				local color = self:num_to_color(self.draw_points[y][x])
				love.graphics.setColor(color[1], color[2], color[3])
				love.graphics.point(self.x+0.5+(x-1), self.y+0.5+(y-1))
				love.graphics.setColor(255,255,255)
			end
		end
	else	
		love.graphics.setColor(self.color[1], self.color[2], self.color[3])
		love.graphics.rectangle("fill", self.x, self.y, game.tile_size, game.tile_size)
	end
end

require'src.resource_states.water'
require'src.resource_states.soil'
require'src.resource_states.plant'